﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace IPConfig
{
    public partial class Form1 : Form
    {
        public XMLHelper xmlHelper;
        public Form1()
        {
            InitializeComponent();
            xmlHelper = new XMLHelper();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                for (int i = 1; i <= 3; i++)
                {
                    IPConfigModel model = xmlHelper.GetIPConfigModel(i);
                    ((TextBox)this.Controls.Find("IP" + i, true)[0]).Text = model.IP;
                    ((TextBox)this.Controls.Find("ZW" + i, true)[0]).Text = model.ZW;
                    ((TextBox)this.Controls.Find("WG" + i, true)[0]).Text = model.WG;
                    ((TextBox)this.Controls.Find("DNSF" + i, true)[0]).Text = model.DNSF;
                    ((TextBox)this.Controls.Find("DNSS" + i, true)[0]).Text = model.DNSS;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


        private void btnSaveIPConfig_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 1; i <= 3; i++)
                {
                    IPConfigModel model = new IPConfigModel();
                    model.IP = ((TextBox)this.Controls.Find("IP" + i, true)[0]).Text;
                    model.ZW = ((TextBox)this.Controls.Find("ZW" + i, true)[0]).Text;
                    model.WG = ((TextBox)this.Controls.Find("WG" + i, true)[0]).Text;
                    model.DNSF = ((TextBox)this.Controls.Find("DNSF" + i, true)[0]).Text;
                    model.DNSS = ((TextBox)this.Controls.Find("DNSS" + i, true)[0]).Text;
                    xmlHelper.SetIPConfigModel(i, model);
                }
                MessageBox.Show("保存配置成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAUTO_Click(object sender, EventArgs e)
        {
            try
            {
                NetworkHelper.SetNetworkDHCP();
                MessageBox.Show("切换配置成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnIPConfig1_Click(object sender, EventArgs e)
        {
            try
            {
                NetworkHelper.SetNetworkAdapter(xmlHelper.GetIPConfigModel(1));
                MessageBox.Show("切换配置成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnIPConfig2_Click(object sender, EventArgs e)
        {
            try
            {
                NetworkHelper.SetNetworkAdapter(xmlHelper.GetIPConfigModel(2));
                MessageBox.Show("切换配置成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnIPConfig3_Click(object sender, EventArgs e)
        {
            try
            {
                NetworkHelper.SetNetworkAdapter(xmlHelper.GetIPConfigModel(3));
                MessageBox.Show("切换配置成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
